package io.gitee.chearnee.fw.datasecurity.sd.extension.ruleimpl;

import io.gitee.chearnee.fw.common.utils.ConstStringPool;
import io.gitee.chearnee.fw.datasecurity.core.enums.NumEnum;
import io.gitee.chearnee.fw.datasecurity.core.handler.desensitization.SDRuleHandler;
import io.gitee.chearnee.fw.datasecurity.sd.extension.annotation.SDEmailField;
import org.springframework.util.StringUtils;

/**
 * @author cn
 * @version 1.0
 * @description
 * @date 11/22/2022 11:18 AM
 */
public class EmailRuleHandler implements SDRuleHandler<SDEmailField> {
    @Override
    public Class<SDEmailField> getAnnotationType() {
        return SDEmailField.class;
    }

    @Override
    public String masking(SDEmailField sdIdCardField, String object) {
        if (StringUtils.hasText(object)) {
            int i = object.lastIndexOf("@");
            if (i < 0) {
                return object;
            }
            String before = object.substring(0, i);
            String after = object.substring(i);
            before = baseMasking(NumEnum.ZERO.getValue(), NumEnum.ZERO.getValue(), ConstStringPool.MASK_STRING, before);
            after = baseMasking(NumEnum.NAN.getValue(), NumEnum.NAN.getValue(), ConstStringPool.MASK_STRING, after);
            return after + before;
        }
        return object;
    }
}
